import { create } from 'zustand'
import { CurrentRoutePathState } from '@/ui/antd/types/model/dto/CurrentRoutePathState'
import { createJSONStorage, persist } from 'zustand/middleware'
import globalConfig from '@/core/framework/config/globalConfig'

const useCurrentRoutePathStore = create(
	persist<CurrentRoutePathState>(
		(setState, getState) => {
			const state = {
				path: undefined,
				setPath: (path: string | undefined): void => {
					setState({ path: path })
				},
			} as CurrentRoutePathState
			return state
		},
		{
			name: `${globalConfig.storageKeyPrefix}.app.layout.current.route`,
			storage: createJSONStorage(() => sessionStorage),
		},
	),
)

export default useCurrentRoutePathStore
